﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.ComponentModel;

namespace BaoVeBienCuong
{
    public partial class Loading : PhoneApplicationPage
    {

        public static ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        private BackgroundWorker bw = new BackgroundWorker();
        Texture2D img_loading;
        Texture2D img_loading_bg;
        Texture2D bg;
        int LoadingPosition = 0;
        double countSoundUpdate = 0;

        public Loading()
        {
            InitializeComponent();
            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(5);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
            AudioManager.Initialize();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bw.RunWorkerAsync();
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;


            // Perform a time consuming operation and report progress.
            // System.Threading.Thread.Sleep(50);
            //   worker.ReportProgress()
            AudioManager.LoadSounds();



        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            LoadingPosition = 380;
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
        public void LoadContent()
        {
            bg = contentManager.Load<Texture2D>("images/menu/menu");
            img_loading = contentManager.Load<Texture2D>("images/loading/fullloading");
            img_loading_bg = contentManager.Load<Texture2D>("images/loading/loadingframe");
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here

            // Start the timer
            timer.Start();
            LoadContent();


            base.OnNavigatedTo(e);
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            //savemusic.
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            countSoundUpdate += e.ElapsedTime.TotalSeconds;
            if (countSoundUpdate >= 0.04f)
            {
                countSoundUpdate = 0;
                if (LoadingPosition < 350)
                    LoadingPosition += 2;
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult m = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "", MessageBoxButton.OKCancel);
            if (m == MessageBoxResult.OK)
            {
                // Application.Current.Terminate();
                Microsoft.Xna.Framework.Game game = new Microsoft.Xna.Framework.Game();
                game.Exit();
            }
            e.Cancel = true;
        }
        private void OnDraw(object sender, GameTimerEventArgs e)
        {

            spriteBatch.Begin();
            spriteBatch.Draw(bg, new Vector2(0, 0), new Rectangle(0, 0, 640, 360), Color.White, 0, Vector2.Zero, 4 / 3.0f, SpriteEffects.None, 0);
            spriteBatch.Draw(img_loading, new Vector2(360, 410), new Microsoft.Xna.Framework.Rectangle(0, 0, LoadingPosition, 47), Color.White, 0f, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0);
            spriteBatch.Draw(img_loading_bg, new Vector2(315, 393), new Microsoft.Xna.Framework.Rectangle(0, 0, 464, 81), Color.White, 0f, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0);
            spriteBatch.End();
        }
    }
}